
package model.math;

import java.util.Random;
import model.info.bigCrimeChance;
import model.info.simpleCrimeChance;

import bean.CharacterBEAN;
import bean.CrimeResultBEAN;
import dao.ActionDAO;
import dao.CharacterDAO;

public class CrimeResult {
	private CharacterDAO	characterDAO	= null;
	private ActionDAO		actionDAO		= null;

	public CrimeResult( ActionDAO actionDAO, CharacterDAO characterDAO ) {
		this.actionDAO = actionDAO;
		this.characterDAO = characterDAO;
	}

	public CrimeResultBEAN doSimpleCrime( int crime, CharacterBEAN character ) {
		boolean win = false, jail = false;
		int cashMoney = 0;
		actionDAO.doSimpleCrime( character.getCharacterID() );
		int chance = getSimpleCrimeChance( crime, character );
		if( getNewRandom() <= chance ) {
			cashMoney = getNewRandom() * ( crime * 10 );
			win = true;
			jail = false;
			characterDAO.setCashMoney( cashMoney + character.getMoneyCash(),
					character.getCharacterID() );
			characterDAO.setXp( ( 50 * crime ) + character.getExperience(),
					character.getCharacterID() );
		} else {
			if( ( 100 - chance ) <= getNewRandom() ) {
				actionDAO.inJail( character.getCharacterID(), character
						.getLevel() );
				characterDAO.setXp( ( 10 * crime + character.getExperience() ),
						character.getCharacterID() );
				jail = true;
				win = false;
			} else {
				characterDAO.setXp( ( 15 * crime ) + character.getExperience(),
						character.getCharacterID() );
			}
		}
		return new CrimeResultBEAN( win, jail, cashMoney );
	}

	public CrimeResultBEAN doBigCrime( int crime, CharacterBEAN character ) {
		boolean win = false, jail = false;
		int cashMoney = 0;
		actionDAO.doBigCrime( character.getCharacterID() );
		int chance = getBigCrimeChance( crime, character );
		if( getNewRandom() <= chance ) {
			cashMoney = getNewRandom() * ( crime * 100 ); // should be better,
															// well it works :p
			win = true;
			jail = false;
			characterDAO.setCashMoney( cashMoney + character.getMoneyCash(),
					character.getCharacterID() );
			characterDAO.setXp( ( 75 * crime ) + character.getExperience(),
					character.getCharacterID() );
		} else {
			if( ( 100 - chance ) <= getNewRandom() ) {
				actionDAO.inJail( character.getCharacterID(), character
						.getLevel() );
				characterDAO.setXp( ( 10 * crime + character.getExperience() ),
						character.getCharacterID() );
				jail = true;
				win = false;
			} else {
				characterDAO.setXp( ( 15 * crime ) + character.getExperience(),
						character.getCharacterID() );
			}
		}
		return new CrimeResultBEAN( win, jail, cashMoney );
	}

	public CrimeResultBEAN doShootArea( CharacterBEAN character ) {
		boolean win = false;
		actionDAO.doShootArea( character.getCharacterID() );
		if( getNewRandom() <= character.getShootExperience() ) {
			win = true;
			characterDAO.setXp( character.getExperience() + 10, character
					.getCharacterID() );
			characterDAO.addShootSkill( character.getCharacterID(), character
					.getShootExperience() + 1 );
		}
		return new CrimeResultBEAN( win, false, 0 );
	}

	private int getSimpleCrimeChance( int crime, CharacterBEAN character ) {
		for( simpleCrimeChance scc : simpleCrimeChance.values() ) {
			if( character.getLevelName( character.getLevel() ).equals(
					scc.toString() ) ) {
				System.out.println( "gotInLoop" );
				if( crime == 1 ) {
					System.out.println( scc.getS1() );
					return scc.getS1();
				} else if( crime == 2 ) {
					System.out.println( scc.getS2() );
					return scc.getS2();
				} else if( crime == 3 ) {
					System.out.println( scc.getS3() );
					return scc.getS3();
				} else if( crime == 4 ) {
					System.out.println( scc.getS4() );
					return scc.getS4();
				} else if( crime == 5 ) {
					System.out.println( scc.getS5() );
					return scc.getS5();
				}
			}
		}
		return 0;
	}

	private int getBigCrimeChance( int crime, CharacterBEAN character ) {
		for( bigCrimeChance scc : bigCrimeChance.values() ) {
			if( character.getLevelName( character.getLevel() ).equals(
					scc.toString() ) ) {
				if( crime == 1 ) {
					return scc.getS1();
				} else if( crime == 2 ) {
					return scc.getS2();
				} else if( crime == 3 ) { return scc.getS3(); }
			}
		}
		return 0;
	}

	private int getNewRandom() {
		Random random = new Random();
		int randint = random.nextInt( 100 );
		System.out.println( "Random int is " + randint );
		return randint;
	}
}
